home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8365 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Using char array in functions
  5. Date: 01 Mar 1996 16:27:39 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Mar1092739@qcd.lanl.gov>
  8. References: <20FEB199609155775@sundog.caltech.edu> <4gqk5m$jno@aphex.direct.ca>
  9.     <1996Feb29.230019.5028@isac.hces.com>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: gg@isac.hces.com's message of Thu, 29 Feb 1996 23:00:19 GMT
  14.  
  15. In article <1996Feb29.230019.5028@isac.hces.com>
  16. gg@isac.hces.com (Greg Goodrich) writes:
  17.  
  18. <snip>
  19. GG: /* declaration */
  20. GG: char array1[100];
  21. GG: 
  22. GG: main()
  23. GG: {
  24. GG:     foo(array1);
  25. GG: }
  26. GG: 
  27. GG: In this example, the function foo() can modify any member of the array
  28. GG: "array1" at will.  You do not have to pass the address of the array
  29. GG: name, because by using the array name, you are actually passing
  30. GG: &array1[0], or in english, the address of the first member of the array,
  31. GG: which for argument's sake is also the address of the array.  This allows
  32.  
  33. It may be the address of the array, but because of past confusion, one
  34. must point out that it is a char *, not char (*)[100]. The proper
  35. `address of the array' is of the latter type.
  36.  
  37. I usually assume, if I have to, that (void*)array == (void*)&array,
  38. though, strictly speaking, the standard does not guarantee that. That
  39. does not mean the two are interchangeable: (double)2 == (double)2.0,
  40. but as anyone who has used C knows, passing one instead of the other
  41. can be a disaster!
  42.  
  43. GG: any manipulation of the array inside the function that the function
  44.  
  45. The reason the manipulation is possible is because given the two
  46. facts:
  47.   1) The array elements are contiguous
  48.   2) The function has a pointer to the first element
  49. it can actually construct the pointer to any element in the array it
  50. needs. Because of this, it can modify any element: and when every
  51. element of an array can be modified, we think of it as `the array can
  52. be modified'.
  53.  
  54. Cheers
  55. Tanmoy
  56. --
  57. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  58. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  59. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  60. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  61. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  62. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  63.